PDO Statement moving away from mysql_connect [on hold]
Posted
by
user3555680
on Pro Webmasters
See other posts from Pro Webmasters
or by user3555680
Published on 2014-06-08T23:21:21Z
Indexed on
2014/06/09
3:43 UTC
Read the original article
Hit count: 156
Can someone please help me write a simple query "SELECT first_name FROM table_name" using PDO. I want to move away from using mysql_connect. Im working on a php project with mysql. Can you please write for me such a code that i can use THANK YOU
<?php
$host="127.0.0.1"; // Host name
$username="root"; // Mysql username
$password=""; // Mysql password
$db_name="microict-intrasys"; // Database name
//$id = 5;
try {
$conn = new PDO('mysql:host={$host};dbname={$db_name}', $username, $password);
// you neeeeeed this--^ and this--^
$stmt = $conn->prepare('SELECT version FROM system_info');
$stmt->execute(array('id' => $id));
$result = $stmt->fetchAll();
if ( count($result) )
{
foreach($result as $row)
{
print_r($row);
}
}
else
{
echo "No rows returned.";
}
}
catch(PDOException $e)
{
echo 'ERROR: ' . $e->getMessage();
}
?>
© Pro Webmasters or respective owner